package hot100.数组转平衡二叉树;

import hot100.TreeNode;

/**
 * @author llt
 * @date 2025/5/13
 */
public class Test {
    public TreeNode sortedArrayToBST(int[] nums) {
        return buildBST(nums, 0, nums.length - 1);
    }

    private TreeNode buildBST(int[] nums, int left, int right) {
        if (left > right) {
            return null;
        }

        // 中间节点作为当前子树根节点
        int mid = left + (right - left) / 2;
        TreeNode root = new TreeNode(nums[mid]);

        // 左子树
        root.left = buildBST(nums, left, mid - 1);

        // 右子树
        root.right = buildBST(nums, mid + 1, right);

        return root;
    }
}
